home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Librarys / MMULib / C_Sources / MuScan.c < prev    next >
C/C++ Source or Header  |  1999-11-27  |  16KB  |  386 lines

  1. /*************************************************************************
  2.  ** mmu.library                                                         **
  3.  **                                                                     **
  4.  ** a system library for arbitration and control of the MC68K MMUs      **
  5.  **                                                                     **
  6.  ** © 1998,1999 THOR-Software, Thomas Richter                           **
  7.  ** No commercial use, reassembly, modification without prior, written  **
  8.  ** permission of the authors.                                          **
  9.  ** Including this library in any commercial software REQUIRES a        **
  10.  ** written permission and the payment of a small fee.                  **
  11.  **                                                                     **
  12.  ** This sample source demonstrates how to print out the mapping of the **
  13.  ** MMU library. It's here done for the default context                 **
  14.  *************************************************************************/
  15.  
  16. /// Includes
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <dos/dos.h>
  20. #include <mmu/mmubase.h>
  21. #include <mmu/context.h>
  22. #include <mmu/config.h>
  23. #include <workbench/startup.h>
  24.  
  25. #include <proto/exec.h>
  26. #include <proto/mmu.h>
  27. #include <proto/dos.h>
  28. #include <proto/icon.h>
  29.  
  30. #include <string.h>
  31. ///
  32. /// Defines
  33. #define STRINGDATE "28.11.99"
  34. #define STRINGVERSION "40.3"
  35. #define TEMPLATE "TO/K"
  36.  
  37. #define OPT_TO  0
  38. #define OPT_WINDOW 1
  39. #define OPT_COUNT 2
  40. ///
  41. /// Statics
  42. struct MMUBase *MMUBase;
  43. struct DosLibrary *DOSBase;
  44. struct ExecBase *SysBase;
  45. struct Library *IconBase;
  46. ///
  47. /// Protos
  48. int __asm __saveds main(void);
  49. void ScanMMU(BPTR to);
  50. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp);
  51. ///
  52.  
  53. char version[]="$VER: MuScan " STRINGVERSION " (" STRINGDATE ") © THOR";
  54.  
  55. /// main
  56. int __asm __saveds main(void)
  57. {
  58. LONG args[OPT_COUNT];
  59. struct RDArgs *rd,*myrd;
  60. struct Process *proc;
  61. int rc=20;
  62. LONG err;
  63. struct WBStartup *msg;
  64. BPTR oldout;
  65. struct MsgPort *oldconsole;
  66. BPTR out;
  67.  
  68.  
  69.         SysBase=*((struct ExecBase **)(4L));
  70.  
  71.         memset(args,0,sizeof(LONG)*OPT_COUNT);
  72.         /* Wait for the workbench startup, if any */
  73.         proc=(struct Process *)FindTask(NULL);
  74.  
  75.         if (!(proc->pr_CLI)) {
  76.                 WaitPort(&(proc->pr_MsgPort));
  77.                 msg=(struct WBStartup *)GetMsg(&(proc->pr_MsgPort));
  78.         } else  msg=NULL;
  79.  
  80.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  81.                 if (MMUBase=(struct MMUBase *)OpenLibrary("mmu.library",40L)) {
  82.  
  83.                         myrd=NULL;      /* reset the temporary ReadArgs */
  84.                         oldout=NULL;
  85.                         oldconsole=NULL;
  86.                         if (msg) {
  87.                                 oldout=SelectOutput(NULL);
  88.                                 oldconsole=SetConsoleTask(NULL);
  89.                                 rd=ReadTTArgs(msg,args,&myrd);
  90.                         } else  rd=ReadArgs(TEMPLATE,args,NULL);
  91.  
  92.                         if (rd) {
  93.                                 /* Argument parser worked, call main routine */
  94.                                 if (args[OPT_TO])       out=Open((char *)(args[0]),MODE_NEWFILE);
  95.                                 else                    out=Output();
  96.  
  97.                                 if (out) {
  98.                                         ScanMMU(out);
  99.                                         err=0;
  100.                                         if (args[OPT_TO])
  101.                                                 Close(out);
  102.                                 } else err=IoErr();
  103.  
  104.                                 FreeArgs(rd);
  105.                                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  106.                                 if (msg)  Close(SelectOutput(NULL));
  107.                         } else  err=IoErr();
  108.  
  109.                         if (msg) {
  110.                                 SelectOutput(oldout);
  111.                                 SetConsoleTask(oldconsole);
  112.                         }
  113.  
  114.                         if (err<64) {
  115.                                 rc=err;
  116.                                 err=0;
  117.                         } else {
  118.                                 if (!msg) PrintFault(err,"MuScan failed");
  119.                                 rc=10;
  120.                         }
  121.                         SetIoErr(err);
  122.  
  123.                         CloseLibrary((struct Library *)MMUBase);
  124.                 } else PrintFault(ERROR_OBJECT_NOT_FOUND,"MuScan requires the mmu.library V40 or better");
  125.                 CloseLibrary((struct Library *)DOSBase);
  126.         }
  127.  
  128.         if (msg) {
  129.                 Forbid();
  130.                 ReplyMsg((struct Message *)msg);
  131.         }
  132.  
  133.         return rc;
  134. }
  135. ///
  136. /// ReadTTArgs
  137. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp)
  138. {
  139. struct WBArg *wbarg;
  140. struct DiskObject *dop;
  141. char **tt;                      /* ToolTypes array */
  142. char *wbstr;                    /* Our self-made workbench argument string */
  143. char *here;
  144. BPTR oldlock;
  145. ULONG len;
  146. struct RDArgs *rd=NULL,*myrd=NULL;
  147. LONG err=0;
  148. BPTR newout;
  149.  
  150.         if (IconBase=OpenLibrary("icon.library",37L)) {
  151.                 if (wbarg=msg->sm_ArgList) {
  152.                         /* use a project icon if there is one... */
  153.                         if (msg->sm_NumArgs > 1) wbarg++;
  154.  
  155.                         /* go into the directory */
  156.                         oldlock=CurrentDir(wbarg->wa_Lock);
  157.  
  158.                         if (dop=GetDiskObject(wbarg->wa_Name)) {
  159.                                 if (tt=dop->do_ToolTypes) {
  160.                                         /* Read a special tool type for the output window */
  161.  
  162.                                         /* Calc the size of the argument string */
  163.  
  164.                                         len = 3;        /* reserve space for SPC,LF,NUL */
  165.                                         while (*tt) {
  166.                                                 len += strlen(*tt)+1;   /* string, plus space */
  167.                                                 tt++;
  168.                                         }
  169.  
  170.                                         if (wbstr=AllocVec(len,MEMF_PUBLIC)) {
  171.                                                 /* Now copy the arguments into this string, one by one
  172.                                                    and check whether the argument string is still valid. */
  173.  
  174.                                                 tt=dop->do_ToolTypes;
  175.                                                 here=wbstr;
  176.                                                 do{
  177.                                                         *here='\0';                     /* terminate string */
  178.                                                         /* Check whether this tool type is
  179.                                                            commented out. Just ignore it in this case */
  180.                                                         if (*tt) {
  181.                                                                 if (**tt=='(' || **tt==';')
  182.                                                                         continue;
  183.  
  184.                                                                 strcpy(here,*tt);      /* Add TT string */
  185.                                                         }
  186.                                                         len=strlen(here);
  187.                                                         here[len]='\n';
  188.                                                         here[len+1]='\0';               /* terminate string */
  189.  
  190.                                                         /* Now try to ReadArg' this string */
  191.  
  192.                                                         /* release old arguments left over from last loop */
  193.                                                         if (rd) FreeArgs(rd);
  194.                                                         if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  195.                                                         rd=NULL;
  196.                                                         memset(args,0,sizeof(LONG)*OPT_COUNT);
  197.  
  198.                                                         if (myrd=AllocDosObject(DOS_RDARGS,NULL)) {
  199.